package com.example.controller;

import com.example.entity.ProductCategory;
import com.example.entity.Result;
import com.example.service.ProductCategoryService;
//import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "商品分类", description = "商品分类的增删改查")
@RestController
@RequestMapping("/api/categories")
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService categoryService;

    @PostMapping
    public Result<ProductCategory> createCategory(@RequestBody ProductCategory category) {
        ProductCategory created = categoryService.createCategory(category);
        return Result.success(created);
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteCategory(@PathVariable Long id) {
        categoryService.deleteCategory(id);
        return Result.fail("查不到指定数据");
    }

    @PutMapping("/{id}")
    public Result<ProductCategory> updateCategory(
            @PathVariable Long id, @RequestBody ProductCategory category) {
        category.setId(id);
        ProductCategory updated = categoryService.updateCategory(category);
        return Result.success(updated);
    }

    @GetMapping("/{id}")
    public Result<ProductCategory> getCategoryById(@PathVariable Long id) {
        ProductCategory category = categoryService.getCategoryById(id);
        return Result.success(category);
    }

    @GetMapping
    public Result<List<ProductCategory>> getAllCategories() {
        List<ProductCategory> categories = categoryService.getAllCategories();
        return Result.success(categories);
    }

    @GetMapping("/name/{name}")
    public Result<ProductCategory> getCategoryByName(@PathVariable String name) {
        ProductCategory category = categoryService.getCategoryByName(name);
        return Result.success(category);
    }
}

